/*
 * Copyright (c) 2006-2021, RT-Thread Development Team
 *
 * SPDX-License-Identifier: Apache-2.0
 *
 * Change Logs:
 * Date           Author       Notes
 * 2021-12-04     Administrator       the first version
 */
#include "uart.h"
#include <string.h>
#include "pwm.h"
#include "iic.h"
struct serial_configure config = RT_SERIAL_CONFIG_DEFAULT;  /* 初始化配置参数 */
/* 用于接收消息的信号量 */
static struct rt_semaphore rx_sem;
static rt_device_t serial;
char uart_receive_data[10];
uint8_t uart_receive_count;
extern  rt_flag_t power;
/**

  * @brief  uart_input //接收数据回调函数

  * @param  dev

  *         size

  * @retval RT_EOK

  */
static rt_err_t uart_input(rt_device_t dev, rt_size_t size)
{
    /* 串口接收到数据后产生中断，调用此回调函数，然后发送接收信号量 */
    rt_sem_release(&rx_sem);
    return RT_EOK;
}

/*
  * @brief uart_send_data
  * @param  parameter
  * @retval None
  */

void uart_send_byte(uint16_t id)
{
       static uint8_t i;
       static uint16_t temp;
       static char uart1_tx_buffer[16]={0xA5,0xFC,0x06,0x00,0xA1,0x92,0x01,0x80,0x92,0x19,0x00,0x00,0x00,0x5F,0x02,0xFB};
       static  rt_uint32_t tx_length=16;
       static char uart_name[8];
      // rt_strncpy(uart_name,"uart1", 8);
     //  rt_device_write( rt_device_find(uart_name), 0,&uart1_tx_buffer[0],tx_length );
       uart1_tx_buffer[9]=id;
       uart1_tx_buffer[10]=id>>8;
      for(i=4;i<13;i++)
      {
         temp += uart1_tx_buffer[i]; //SUM
      }
      uart1_tx_buffer[13] = temp ; //低在前
      uart1_tx_buffer[14] = temp>>8;
      rt_strncpy(uart_name,"uart1", 8);
      rt_device_write( rt_device_find(uart_name), 0,&uart1_tx_buffer[0],tx_length );
      temp=0;
}
/*
  * @brief  serial_thread_entry
  * @param  parameter
  * @retval None
  */

static void serial_thread_entry(void *parameter)
{
    char ch;
    while (1)
    {
        /* 从串口读取一个字节的数据，没有读取到则等待接收信号量 */
       while (rt_device_read(serial, -1, &ch,1) != 1)
        {
            /* 阻塞等待接收信号量，等到信号量后再次读取数据 */
            rt_sem_take(&rx_sem, RT_WAITING_FOREVER);
        }
        /* 读取到的数据输出 */
       if (uart_receive_count>9) uart_receive_count=0;
     // rt_kprintf("%d",uart_receive_count);
       uart_receive_data[uart_receive_count++]=ch;
     //  rt_kprintf("%c",ch);
      if(     uart_receive_data[uart_receive_count-1]==0xdc&&
              uart_receive_data[uart_receive_count-2]==0xcd&&
              uart_receive_data[uart_receive_count-5]==0xba&&
              uart_receive_data[uart_receive_count-6]==0xab )
      {
        //  rt_kprintf("uart_data is ok \r\n");

//          rt_kprintf("%c",uart_receive_data[uart_receive_count-4]);
//          rt_kprintf("%c",uart_receive_data[uart_receive_count-3]);
          if(uart_receive_data[uart_receive_count-4]==0xaa)//功能
                  {
          switch(uart_receive_data[uart_receive_count-3])
          {
          case 0x01:       pwm_vout_adj(35,1) ;  break; //1档
          case 0x02:       pwm_vout_adj(40,1) ;  break; //2档
          case 0x03:       pwm_vout_adj(45,1) ;break; //3档
          case 0x04:       pwm_vout_adj(50,1) ; break; //4档
          case 0x05:       pwm_vout_adj(55,1) ;break; //5档
          case 0x06:       pwm_vout_adj(60,1) ; break; //6档
          case 0x07:       pwm_vout_adj(65,1) ; break; //7档
          case 0x08:       pwm_vout_adj(70,1) ;break; //8档
          case 0x09:       pwm_vout_adj(75,1) ;break; //9档
          case 0x10:       pwm_vout_adj(80,1) ; break; //10档
          case 0x011:      pwm_vout_adj(85,1) ;break; //11档
          case 0x012:      pwm_vout_adj(90,1) ;break; //12档
          case 0x013:      pwm_vout_adj(95,1) ; break; //13档
          case 0x16:    power=1; pwm_vout_adj(35,1);break; //开机
          case 0x17:    power=0 ;pwm_vout_adj(35,0) ;break; //关机
          case 0xbb:    if(pwm_data<35)pwm_data=35;  pwm_vout_adj(pwm_data-=5,1); ;break; //减档
          case 0xaa:    if(pwm_data>95)pwm_data=95;  pwm_vout_adj(pwm_data+=5,1); ;break; //加档
          default :    ; break;
          }
                  }
          uart_receive_count=0;
       //  uart_send_byte(0x6000);
      }
    }
}




/*
  * @brief  uart_send
  * @param  channel
  * @retval str
  */
void uart_send(const char *channel, char *str)
{
        static char uart_name[8];
       // rt_uint16_t len = strlen(str);
       // rt_kprintf("str =%d\r\n",len);
        rt_strncpy(uart_name,channel, 8);
        rt_device_write( rt_device_find(uart_name), 0, str, strlen(str) );

}


/*
  * @brief  thread_serial
  * @param  None
  * @retval ret
  */
int thread_serial(void)
{
    rt_err_t ret = RT_EOK;
    char uart_name[RT_NAME_MAX];
    char str[] = "uart1 OK \r\n";
    rt_strncpy(uart_name, SAMPLE_UART_NAME, RT_NAME_MAX);
    /* 查找系统中的串口设备 */
    serial = rt_device_find(uart_name);
    if (!serial)
    {
        rt_kprintf("find %s failed!\n", uart_name);
        return RT_ERROR;
    }
    /* 修改串口配置参数 */
    config.baud_rate = BAUD_RATE_115200;        //修改波特率为 9600
    config.data_bits = DATA_BITS_8;           //数据位 8
    config.stop_bits = STOP_BITS_1;           //停止位 1
    config.bufsz     = 64;                   //修改缓冲区 buff size 为 128
    config.parity    = PARITY_NONE;           //无奇偶校验位
    /* 控制串口设备。通过控制接口传入命令控制字，与控制参数 */
    rt_device_control(serial, RT_DEVICE_CTRL_CONFIG, &config);
    /* 初始化信号量 */
    rt_sem_init(&rx_sem, "rx_sem", 0, RT_IPC_FLAG_FIFO);
    /* 以中断接收及轮询发送模式打开串口设备 */
    rt_device_open(serial, RT_DEVICE_FLAG_INT_RX);
    /* 设置接收回调函数 */
    rt_device_set_rx_indicate(serial, uart_input);
    /* 发送字符串 */
     rt_device_write(serial, 0, str, (sizeof(str) - 1));

    /* 创建 serial 线程 */
    rt_thread_t thread = rt_thread_create("serial", serial_thread_entry, RT_NULL, 1024, 25, 10);
    /* 创建成功则启动线程 */
    if (thread != RT_NULL)
    {
        rt_thread_startup(thread);
    }
    else
    {
        ret = RT_ERROR;
    }
    return ret;
}

/* 导出到 msh 命令列表中 */
//MSH_CMD_EXPORT(thread_serial, uart device sample);
